package com.solcoo.merchant.base.task;

import java.util.Map;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.solcoo.common.network.HttpDownloader;
import com.solcoo.common.network.HttpResult;
import com.solcoo.common.util.ArrayUtil;
import com.solcoo.common.util.StringUtil;

/**
 * 
 * 提供异步的网络请求封装类
 * 
 * @author panjunlin
 * 
 */
public class TaskHelper {

	public static <T extends IHttpResultWapper> void loadView(Context context,
			String url, Callback<T> callback, Class<T> clazz) {
		new NetworkLoadView<T>(context, callback, clazz).execute(url);
	}

	public static <T extends IHttpResultWapper> void oper(Context context,
			String url, Callback<T> callback, Class<T> clazz) {
		new NetworkOper<T>(context, callback, clazz).execute(url);
	}

	public static <T extends IHttpResultWapper> void oper(Context context,
			String url, Callback<T> callback, Class<T> clazz,
			Map<String, Object> params) {
		new NetworkOper<T>(context, callback, clazz).execute(url, params);
	}

	public interface IHttpResultWapper {
		boolean isSuccess();
	}

	public interface Callback<T> {
		void onSuccess(T result);

		void onFail(T result);

	}

	private static class NetworkLoadView<T extends IHttpResultWapper> extends
			LoadViewAsyncTask<String, Void, T> {

		Callback<T> mCallback;

		Class<T> type;

		public NetworkLoadView(Context context, Callback<T> callback,
				Class<T> clazz) {
			super(context);
			mCallback = callback;
			type = clazz;
		}

		@Override
		protected T doInBackground(String... params) {
			HttpResult result = HttpDownloader.getConnectData(params[0]);
			if (result.isSuccess() && result.data != null
					&& result.data.length > 0) {
				return JSON.parseObject(result.data, type);
			} else {
				return null;
			}
		}

		@Override
		protected void onPostExecute(T result) {
			super.onPostExecute(result);
			mCallback.onSuccess(result);
		}

		@Override
		protected void onNullData() {
			super.onNullData();
			mCallback.onFail(null);
		}
	}

	private static class NetworkOper<T extends IHttpResultWapper> extends
			BindContextAsyncTask<Object, Void, T> {

		Callback<T> mCallback;

		Class<T> type;

		public NetworkOper(Context context, Callback<T> callback, Class<T> clazz) {
			super(context);
			mCallback = callback;
			type = clazz;
		}

		@SuppressWarnings("unchecked")
		@Override
		protected T doInBackground(Object... params) {
			Map<String, Object> mapParams = (Map<String, Object>) ArrayUtil
					.get(params, 1);
			String url = StringUtil.objectToString(params[0]);
			HttpResult result = null;
			if (mapParams != null) {
				result = HttpDownloader.post(url, mapParams);
			} else {
				result = HttpDownloader.getConnectData(url);
			}

			if (result.isSuccess() && result.data != null
					&& result.data.length > 0) {
				Log.v(TAG, "NetworkOper result:" + new String(result.data));
				return JSON.parseObject(result.data, type);
			} else {
				return null;
			}
		}

		@Override
		protected void onPostExecute(T result) {
			super.onPostExecute(result);
			if (result == null) {
				return;
			}
			if (result != null && result.isSuccess()) {
				mCallback.onSuccess(result);
			} else {
				mCallback.onFail(result);
			}
		}

		@Override
		public long getTimeOut() {
			return LONG_TIME_OUT;
		}
	}

}
